import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet("/do")
public class doServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=utf-8");
        HttpSession session = request.getSession();

        String[] Goods = request.getParameterValues("goods");
        if(Goods != null){
            for(String goods : Goods){
                if("goods1".equals(goods)){
                    Object g1 = session.getAttribute("goods1");
                    if(g1 == null){
                        session.setAttribute("goods1","1");
                    }else{
                        Integer value = Integer.valueOf(g1.toString());
                        session.setAttribute("goods1",value + 1);
                    }
                }else if("goods2".equals(goods)){
                    Object g2 = session.getAttribute("goods2");
                    if(g2 == null){
                        session.setAttribute("goods2","1");
                    }else{
                        Integer value = Integer.valueOf(g2.toString());
                        session.setAttribute("goods2",value + 1);
                    }
                }else if("goods3".equals(goods)){
                    Object g3 = session.getAttribute("goods3");
                    if(g3 == null){
                        session.setAttribute("goods3","1");
                    }else{
                        Integer value = Integer.valueOf(g3.toString());
                        session.setAttribute("goods3",value + 1);
                    }
                }
            }
        }
        response.sendRedirect("cart.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
